package com.liuya.safe.policy.handler;

import com.liuya.base.BaseModel;
import com.liuya.common.WebUtil;
import com.liuya.safe.policy.service.BusinessDataManagerService;
import com.liuya.safe.model.SafeBusinessData;
import com.liuya.safe.tree.AbstractTreeHandler;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Collection;
import java.util.Iterator;

/**
 * 策略业务树处理类
 */
@Component("businessDataTreeHandler")
public class BusinessDataTreeHandler extends AbstractTreeHandler {

    @Resource(name="businessDataManagerService")
    private BusinessDataManagerService busiManagerService;

    @Override
    public String getPageTitle() {
        return "Business_Data_Tree";
    }

    @Override
    public String getCreatePage() {
        return "policy/businessData/create";
    }

    @Override
    public String getNodeName(BaseModel node) {
        SafeBusinessData bd = (SafeBusinessData) node;
        return bd.getName();
    }

    @Override
    public Collection getAllNodes() {
        return busiManagerService.getAllBusinessData();
    }

    @Override
    public BaseModel getNode(HttpServletRequest req) {
        SafeBusinessData bd = new SafeBusinessData();
        String id = getId(req);
        String pid = getParentId(req);
        String name = req.getParameter("name");
        String desc = req.getParameter("description");
        boolean isLeaf = WebUtil.getBooleanParameter(req, "isLeaf", false);

        bd.setId(id);
        bd.setIsLeaf(isLeaf);
        bd.setName(name);
        bd.setPid(pid);
        bd.setDescription(desc);
        return bd;
    }

    @Override
    public BaseModel getNodeByName(String name) {
        Collection data = busiManagerService.getAllBusinessData();
        for (Iterator iter = data.iterator(); iter.hasNext();) {
            SafeBusinessData bizData = (SafeBusinessData) iter.next();
            if (bizData.getName().equals(name)) {
                return bizData;
            }
        }

        return null;
    }

    @Override
    public boolean addNode(Object node) {
        SafeBusinessData bd = (SafeBusinessData) node;
        try{
            busiManagerService.addBusinessData(bd.getPid(), bd.getName(), bd.getDescription(), bd.getIsLeaf());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }

    @Override
    public boolean deleteNode(String id) {
        busiManagerService.deleteBusinessData(id);
        return true;
    }

    @Override
    public boolean updateNode(Object node) {
        SafeBusinessData bd = (SafeBusinessData) node;
        try{
            busiManagerService.updateBusinessData(bd.getId(), bd.getName(), bd.getDescription());
        } catch (Exception e){
            e.printStackTrace();
        }

        return true;
    }

    @Override
    public boolean moveNode(String parentId, String id, int position) {
        busiManagerService.moveBusinessData(id, parentId);
        return true;
    }

    @Override
    public String getContextMenuPage() {
        return "policy/businessData/contextMenu";
    }

    @Override
    public BaseModel getNodeById(String id) {
        return busiManagerService.getBusinessData(id);
    }

    @Override
    public String getModifyPage() {
        return "policy/businessData/modify";
    }

    @Override
    public String getTreeType() {
        return "businessData";
    }

    @Override
    public boolean copyNode(Object node) {
        SafeBusinessData bd = (SafeBusinessData) node;
        try{
            busiManagerService.copyBusinessData(bd.getId(), bd.getName(), bd.getDescription());
        } catch(Exception e){
            e.printStackTrace();
        }
        return true;
    }
}
